package com.yanqu.road.server.timer;

import com.yanqu.road.dao.db.DbMonitor;
import com.yanqu.road.entity.player.UserPass;
import com.yanqu.road.logic.bussiness.DaoHelper;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.executor.GeneralTask;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.math.BigInteger;
import java.util.List;

/**
 * @Author wpj
 * @Date 2020/10/29
 */
public class CheckSqlConnectionTask extends GeneralTask {

    public static final Logger checkSqlConnectLog = LogManager.getLogger("MYSQL_CONNECTIONS");

    @Override
    public void doRun() {
        long frequent = 0;
        try {
            UserPass userPass = DaoHelper.getUserPassDaoImpl().getUserPass(1);
            if (userPass != null) {
                long gcTimes = 0;
                List<GarbageCollectorMXBean> gcList = ManagementFactory.getGarbageCollectorMXBeans();
                for (GarbageCollectorMXBean gc : gcList) {
                    String name = gc.getName();
                    if (name.contains("MarkSweep")) {
                        gcTimes = gc.getCollectionCount();
                    }
                }
                long lastTime = userPass.getLoseBlood().longValue();
                if (lastTime == 0) {
                    lastTime = System.currentTimeMillis();
                    userPass.setLoseBlood(BigInteger.valueOf(System.currentTimeMillis()));
                    userPass.setBuyBuffTimes((int) gcTimes);
                }
                if (lastTime != 0 && System.currentTimeMillis() - lastTime > 60 * 1000) {
                    //1分钟计算一次
                    int times = userPass.getBuyBuffTimes();
                    frequent = gcTimes - times;
                    Logger logger = DbMonitor.sqlLog;
                    if (frequent >= 3) {
                        logger.debug("gc too frequent,times {}", frequent);
                    } else {
                        logger.debug("gc normal,times {}", frequent);
                    }
                    userPass.setBuyBuffTimes((int) gcTimes);
                    userPass.setLoseBlood(BigInteger.valueOf(System.currentTimeMillis()));
                }
            } else {
                userPass = new UserPass();
                userPass.setLoseBlood(BigInteger.valueOf(System.currentTimeMillis()));
            }

            boolean success = true;
            userPass.setUserId(1);
            userPass.setPassId(1);
            userPass.setPassProgress(1);
            userPass.setItemBuyBuffTimes(0);
            userPass.setModifyTime(System.currentTimeMillis());
            if (!DaoHelper.getUserPassDaoImpl().checkDbRight(userPass)) {
                success = false;
            }

            userPass = DaoHelper.getUserPassDaoImpl().getUserPass(1);
            if (userPass == null) {
                success = false;
            }
            if (!success) {
                checkSqlConnectLog.info("sql connect exception");
                GamePlayerMgr.setDbConnect(false);
            } else {
                checkSqlConnectLog.info("sql connect is right");
                GamePlayerMgr.setDbConnect(true);
            }
        } catch (Exception ex) {
            checkSqlConnectLog.error("sql close connection exception");
            GamePlayerMgr.setDbConnect(false);
        }


    }
}
